//
//  NSString+Regex.m
//  SharePlugins
//
//  Created by LiGuicai on 15/7/17.
//  Copyright (c) 2015年 guicai.li.china@gmail.com. All rights reserved.
//

#import "NSString+Regex.h"

@implementation NSString(Regex)
//时间转换为年龄
+ (NSString *)timeConversionAge:(NSString *)str{

    NSInteger age = [NSDate ageWithTimeinterval:[str longLongValue]];
    if (age == 0) {
        return @"";
    }
    return [NSString stringWithFormat:@"%ld", (long)age];

}

//时间转换为日期
+ (NSString *)timeConversionDate:(NSString *)str{
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[str doubleValue]];
    NSString *timeStr = [confromTimesp stringTimesAgo];
    return  timeStr;
    
}

//判断是否含有emoji表情
+ (BOOL)stringContainsEmoji:(NSString *)string{
    __block BOOL returnValue = NO;
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                const unichar hs = [substring characterAtIndex:0];
                                if (0xd800 <= hs && hs <= 0xdbff) {
                                    if (substring.length > 1) {
                                        const unichar ls = [substring characterAtIndex:1];
                                        const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                        if (0x1d000 <= uc && uc <= 0x1f77f) {
                                            returnValue = YES;
                                        }
                                    }
                                } else if (substring.length > 1) {
                                    const unichar ls = [substring characterAtIndex:1];
                                    if (ls == 0x20e3) {
                                        returnValue = YES;
                                    }
                                } else {
                                    if (0x2100 <= hs && hs <= 0x27ff) {
                                        returnValue = YES;
                                    } else if (0x2B05 <= hs && hs <= 0x2b07) {
                                        returnValue = YES;
                                    } else if (0x2934 <= hs && hs <= 0x2935) {
                                        returnValue = YES;
                                    } else if (0x3297 <= hs && hs <= 0x3299) {
                                        returnValue = YES;
                                    } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                                        returnValue = YES;
                                    }
                                }
                            }];
    
    return returnValue;
}
//判断字符串是否是纯数字的正则表达式
+ (BOOL)isNumText:(NSString *)str{
    NSString * regex        = @"(/^[0-9]*$/)";
    NSPredicate * pred      = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch            = [pred evaluateWithObject:str];
    if (isMatch) {
        return YES;
    }else{
        return NO;
    }
    
}
+ (BOOL) isEmpty:(NSString *) str {
    
    if (!str) {
        return true;
    } else {
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}


/**
 *  匹配邮箱
 */
- (BOOL)isEmail:(NSString *)str {
    NSString * regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    return [self checkStringWithRegex:regex checkString:str];
}

/**
 *  匹配手机号码
 */
+ (BOOL)isPhoneNumber:(NSString *)str {
    if (str.length != 11) {
        return NO;
    }
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(str.length > 0) {
        return NO;
    }
    return YES;
}
//- (BOOL)isPhoneNumber:(NSString *)str {
//    /**
//     * 手机号码
//     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
//     * 联通：130,131,132,152,155,156,185,186
//     * 电信：133,1349,153,180,189
//     */
//    NSString * regex = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
//
//    return [self checkStringWithRegex:regex checkString:str];
//}

/**
 *  匹配中国移动手机号码
 */
- (BOOL)isCMPhoneNumber:(NSString *)str {
    NSString * regex = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    return [self checkStringWithRegex:regex checkString:str];
}

/**
 *  匹配中国联通手机号码
 */
- (BOOL)isCUPhoneNumber:(NSString *)str {
    NSString * regex = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    return [self checkStringWithRegex:regex checkString:str];
}

/**
 *  匹配中国电信手机号码
 */
- (BOOL)isCTPhoneNumber:(NSString *)str {
    NSString * regex = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    return [self checkStringWithRegex:regex checkString:str];
}

/**
 *  匹配国内电话号码
 */
- (BOOL)isCallNumber:(NSString *)str {
    NSString * regex = @"d{3}-d{8}|d{4}-d{7}";
    return [self checkStringWithRegex:regex checkString:str];
}

/**
 *  匹配QQ号码
 */
- (BOOL)isQQ:(NSString *)str {
    NSString * regex = @"[1-9][0-9]{4,}";
    return [self checkStringWithRegex:regex checkString:str];
}

/**
 *  匹配网址URL
 */
- (BOOL)isURL:(NSString *)str {
    NSString * regex = @"[a-zA-z]+://[^s]*";
    return [self checkStringWithRegex:regex checkString:str];
}

/**
 *  匹配帐号是否合法(字母开头，允许5-16字节，允许字母数字下划线)
 */
- (BOOL)isLegalCount:(NSString *)str {
    NSString * regex = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
    return [self checkStringWithRegex:regex checkString:str];
}

/**
 *  匹配中国邮政编码
 */
- (BOOL)isPostcode:(NSString *)str {
    NSString * regex = @"[1-9]d{5}(?!d)";
    return [self checkStringWithRegex:regex checkString:str];
}

/**
 *  匹配身份证
 */
- (BOOL)isIDCardNumber:(NSString *)str {
    NSString * regex = @"d{15}|d{18}";
    return [self checkStringWithRegex:regex checkString:str];
}

/**
 *  匹配ip地址
 */
-(BOOL)isIpAddress:(NSString *)str {
    NSString * regex = @"d+.d+.d+.d+";
    return [self checkStringWithRegex:regex checkString:str];
}

#pragma mark - private

- (BOOL)checkStringWithRegex:(NSString *)regex checkString:(NSString *)checkString {
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:checkString];
}

@end
